﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyBanHang_NewApp.DBAccess;

namespace QuanLyBanHang_NewApp.DAL
{
    public class HoaDonDAL
    {
        private dbConnection conn;

        public HoaDonDAL()
        {
            conn = new dbConnection();
        }

        //public bool insertHD(long manv, long makh, string ngaylap, double tonggia, double tienthe)
        //{
        //    string query = "insert into HOADON values(@manv, @makh, @ngaylap, @tonggia, @tienthe)";
        //    SqlParameter[] p = new SqlParameter[5];
        //    p[0] = new SqlParameter("@manv", manv);
        //    p[1] = new SqlParameter("@makh", makh);
        //    p[2] = new SqlParameter("@ngaylap", ngaylap);
        //    p[3] = new SqlParameter("@tonggia", tonggia);
        //    p[4] = new SqlParameter("@tienthe", tienthe);

        //    return conn.execInsertQuery(query, p);
        //}
        public bool insertHD(out long mahd, long manv, long makh, double tonggia, double tienthe)
        {
            string query = "insertHD";
            SqlParameter[] p = new SqlParameter[6];
            p[0] = new SqlParameter("@manv", manv);
            if (makh != 0) p[1] = new SqlParameter("@makh", makh);
            else
            {
                p[1] = new SqlParameter("@makh",SqlDbType.BigInt);
                p[1].Value = DBNull.Value;

            }
            p[2] = new SqlParameter("@ngaylap", DateTime.Now.Date);
            p[3] = new SqlParameter("@tong", tonggia);
            p[4] = new SqlParameter("@tienthe", tienthe);
            p[5] = new SqlParameter("@mahd", SqlDbType.BigInt);
            p[5].Direction = ParameterDirection.Output;
            bool kq = conn.execInsertProcedureQuery(query, p);
            mahd = int.Parse(p[5].Value.ToString());
            return kq;
        }

        public bool insertCTHD(long mahd, long masp, long soluong, double dongia)
        {
            string query = "insert into CHITIETHOADON values(@mahd, @masp, @soluong, @dongia)";
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@mahd", mahd);
            p[1] = new SqlParameter("@masp", masp);
            p[2] = new SqlParameter("@soluong", soluong);
            p[3] = new SqlParameter("@dongia", dongia);

            return conn.execInsertQuery(query, p);
        }

        public DataTable getInfo(int index, int thang, int nam)
        {
            string query;
            SqlParameter[] p = new SqlParameter[2];
            p[0] = new SqlParameter("@thang", thang);
            p[1] = new SqlParameter("@nam", nam);
            switch(index)
            {
                case 0:
                    {
                        query = "NhapXuat";
                        return conn.execSelectProcedureQuery(query, p);
                    }
                case 1:
                    {
                        query = "XemHD";
                        return conn.execSelectProcedureQuery(query, p);
                    }
                case 2:
                    {
                        query = "XemPN";
                        return conn.execSelectProcedureQuery(query, p);
                    }
                default: return null;
            }
        }
    }
}
